package cn.neusoft.dao.impl;

import cn.neusoft.bean.Business;
import cn.neusoft.bean.Orders;
import cn.neusoft.dao.IOrdersDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.query.OrdersQuery;
import cn.neusoft.utils.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersDao implements IOrdersDao {

    private QueryRunner queryRunner;

    public OrdersDao() {
        queryRunner = JdbcUtils.getQueryRunner();
    }

    @Override
    public int add(Orders orders) throws SQLException {
        String sql = "insert into orders(userId, businessId, orderDate, orderTotal, daId, orderState) VALUES (?,?,?,?,?,?)";
        return queryRunner.update(sql,
                orders.getUserId(),
                orders.getBusinessId(),
                orders.getOrderDate(),
                orders.getOrderTotal(),
                orders.getDaId(),
                orders.getOrderState());
    }

    @Override
    public int update(Orders orders) throws SQLException {
        String sql = "update orders set userId=?,businessId=?,orderDate=?,orderTotal=?,daId=?,orderState=? where orderId=?";
        return queryRunner.update(sql,
                orders.getUserId(),
                orders.getBusinessId(),
                orders.getOrderDate(),
                orders.getOrderTotal(),
                orders.getDaId(),
                orders.getOrderState(),
                orders.getOrderId());
    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from orders where orderId=?";
        return queryRunner.update(sql,id);
    }

    @Override
    public Orders getById(Integer id) throws SQLException {
        String sql = "select orderId,userId, businessId, orderDate, orderTotal, daId, orderState from orders where orderId=?";
        return queryRunner.query(sql,new BeanHandler<Orders>(Orders.class),id);
    }

    @Override
    public List<Orders> getAll() throws SQLException {
        String sql = "select orderId,userId, businessId, orderDate, orderTotal, daId, orderState from orders";
        return queryRunner.query(sql,new BeanListHandler<Orders>(Orders.class));
    }

    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        //传递的真实类型是BusinessQuery  所以强转  目的是获取搜索条件
        OrdersQuery query = (OrdersQuery)baseQuery;
        //填充?的参数集合  因为sql是动态拼接的  所以?的数量不确定 有需要就加入到集合  不需要则不加入
        List<Object> params = new ArrayList<>();
        String sql = "select count(orderId) from orders where 1=1 ";
        if(!StringUtils.isEmpty(query.getUserId())) {
            sql += " and userId like concat('%',?,'%')";
            params.add(query.getUserId());
        }
        //动态sql  根据条件参数的传递来确定要不要拼接sql
        if (query.getBusinessId()!=null && query.getBusinessId()!=0){
            sql += " and businessId like concat('%',?,'%')";
            params.add(query.getBusinessId());
        }
        System.out.println("查询总记录："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql, new ScalarHandler<Long>(),params.toArray()).intValue();
    }

    @Override
    public List<Orders> getByPage(BaseQuery baseQuery) throws SQLException {
        //传递的真实类型是BusinessQuery  所以强转  目的是获取搜索条件
        OrdersQuery query = (OrdersQuery)baseQuery;
        //填充?的参数集合  因为sql是动态拼接的  所以?的数量不确定 有需要就加入到集合  不需要则不加入
        List<Object> params = new ArrayList<>();
        //可以动态拼接条件搜索的部分  where 1=1
        String sql = "select orderId,userId, businessId, orderDate, orderTotal, daId, orderState from orders" +
                " where 1=1";
        //动态sql  根据条件参数的传递来确定要不要拼接sql
        if(!StringUtils.isEmpty(query.getUserId())) {
            sql += " and userId like concat('%',?,'%')";
            params.add(query.getUserId());
        }
        //动态sql  根据条件参数的传递来确定要不要拼接sql
        if (query.getBusinessId()!=null && query.getBusinessId()!=0){
            sql += " and businessId like concat('%',?,'%')";
            params.add(query.getBusinessId());
        }
        sql += " ORDER BY orderId DESC limit ?,?";
        //给limit后面的两个?填充的  一直都有
        params.add(query.getStartIndex());
        params.add(query.getPageCount());

        System.out.println("查询集合："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql, new BeanListHandler<Orders>(Orders.class), params.toArray());
    }
}
